<?php
class AdminController extends Controller
{
    //public $layout='column1';
    //public $defaultAction = 'login';

    public function actionIndex(){    
        $this->actionList();
    }

    public function actionList(){ 
        $rows = array();
        $rows = Admin::model()->getRows();  
        $this->render("list",array("rows"=>$rows));
    }

    public function actionLogin(){
        $url = new Url();
        $model = new AdminLogin();
        $this->layout="column_login";
        if(isset($_POST["AdminLogin"])){
            $model->username = $_POST["AdminLogin"]["username"];
            $model->password = $_POST["AdminLogin"]["password"];
            $model->remember = $_POST["AdminLogin"]["remember"];
            if($model->validate()){
                $identity=new UserIdentity($model->username,$model->password);
                if($identity->authenticate()){
                    Yii::app()->user->login($identity);
                    $this->redirect($url->createUrl("home/index"));
                }
                else{
                    echo $identity->errorMessage;  
                }
            }
        }
        $this->render("login",array("model"=>$model));
    }

    public function actionCreate(){
        $url = new Url();
        $user = Yii::app()->user->name;
        if(isset($_POST["username"]) && isset($_POST["password"])){
            $username = $_POST["username"];
            $password = $_POST["password"];
            $rowUser = Admin::getRowsByUsername($username);
            if(!$rowUser){

                $result = Admin::insertRow($username,$password,$username,$user);
                if($result==1){              
                    echo 3;exit();
                }else{
                    echo 1;exit();
                }
            }else{
                echo 2;exit();
            }
        }else{
            $this->render("create",array());
        }
    }
    public function actionEdit(){
        $id = isset($_GET["id"]) ? intval($_GET["id"]):0;
        $row = Admin::getRowsById($id);

        $this->render("create",array("row"=>$row));
    } 

    public function actionUpdate(){
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        $password = isset($_POST["password"]) ? $_POST["password"]:"";
        if($password !=""){
            $pass_db = AdminLogin::hashPassword($password);
            $result = Admin::updateRowById($id,$pass_db);
            echo 1 ;exit();
        }else{
            echo 0;exit();
        }
    }
    public function actionDelete(){
        $id = isset($_POST["id"]) ? intval($_POST["id"]):0;
        if($id !=0){
            $result = Admin::deleteRowById($id);
            echo 1;exit();
        }else{
            echo 0;exit(); 
        }
    }
    public function actionPermit(){
        $id = isset($_GET["id"]) ? intval($_GET["id"]):0;
        $rowAdmin = Admin::getRowsById($id); 
        $rows = Admin::getPermit($id);
        $arr_item = array();
        for($i=0;$i<count($rows);$i++){
            $arr_item[] = $rows[$i]["itemname"];
        }
        //var_dump($arr_item);
        $this->render("permit",array("rows"=>$rows,"rowAdmin"=>$rowAdmin,"arr_item"=>$arr_item,"user_id"=>$id));
    }
    public function actionNoPermit(){
        $this->render("no_permit");
    }
    public function actionSavePermit(){
        $arr_permit = array();
        $arr_add = array();
        $arr_del = array();
        $str_id = isset($_POST["str_id"])  ? rtrim($_POST["str_id"],","):"";
        $user_id = isset($_POST["user_id"]) ? intval($_POST["user_id"]):0;       
        $arr_item = explode(",",$str_id);
        $data_permit = Admin::getPermit($user_id);
        for($i=0;$i<count($data_permit);$i++){
            $arr_permit[] = $data_permit[$i]["itemname"];
        }  
        $arr_add = array_diff($arr_item,$arr_permit);         
        $arr_del = array_diff($arr_permit,$arr_item);      
        $result = Admin::insertPermit($user_id,$arr_add);
        $result2 = Admin::deletePermit($user_id,$arr_del);
        echo 1;exit();
    }

    public function actionLogout(){
        $url = new Url();
        Yii::app()->user->logout();
        $this->redirect($url->createUrl("admin/login"));
    }

    public function actionError(){

    }


}
